package net.lanelife.casino.caipiao.service;

import net.lanelife.casino.caipiao.entity.BalanceChangeReason;
import net.lanelife.casino.caipiao.exception.BalanceChangeReasonCodeExistsException;
import net.lanelife.casino.caipiao.repository.api.IBalanceChangeReasonRepository;
import net.lanelife.casino.caipiao.service.api.IBalanceChangeReasonService;
import net.lanelife.casino.common.dto.support.IGenericDTO;
import net.lanelife.casino.common.repository.support.GenericRepository;
import net.lanelife.casino.common.service.support.GenericService;
import net.lanelife.casino.common.utils.BeanMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BalanceChangeReasonService extends GenericService<BalanceChangeReason, Integer> implements IBalanceChangeReasonService {
	
	@Autowired
	private IBalanceChangeReasonRepository repository;
	
	public BalanceChangeReasonService() {
		super(BalanceChangeReason.class);
	}
	
	@Override
	protected GenericRepository<BalanceChangeReason, Integer> getRepository() {
		return repository;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public <DTO extends IGenericDTO<Integer>> void save(IGenericDTO<Integer> dto) throws Exception {
		if (dto.getId() == null) {
			BalanceChangeReason entity = BeanMapper.map(dto, BalanceChangeReason.class);
			if (repository.findByCode(entity.getCode()) != null) {
				throw BalanceChangeReasonCodeExistsException.ERROR;
			}
		}
		super.save(dto);
	}
	
	

}
